package com.cdut.logistics.action.businesses;



import java.util.List;

import javax.servlet.http.HttpSession;

import com.aliyuncs.exceptions.ClientException;
import com.cdut.logistics.action.message.MessageUtils;
import com.cdut.logistics.action.system.GenerateAccessToken;
import com.cdut.logistics.config.Constants;
import com.cdut.logistics.intercepter.system.AuthorityIntercepter;
import com.cdut.logistics.model.dao.datacentre.Area;
import com.cdut.logistics.model.entity.Model.Academy;
import com.cdut.logistics.model.entity.Model.Dorm;
import com.cdut.logistics.model.entity.Model.Major;
import com.cdut.logistics.model.entity.Model.User;
import com.cdut.logistics.service.impl.businesses.UserServiceImp;
import com.cdut.logistics.service.interf.businesses.UserService;
import com.jfinal.aop.Before;
import com.jfinal.core.Controller;
@Before(AuthorityIntercepter.class)
public class UserController1  extends Controller{
	
	UserService userService = new UserServiceImp();
	
	/**
	 * 
	 */
	public void login()
	{
		System.out.println("2222222222222222222");
		String name = getPara("name");
		String password  =getPara("password");
		//调用service
	//	System.out.println("2222222222222222222");
		String access_token=GenerateAccessToken.generateAccessToken(name);
		userService.addAccess_token(access_token,name);
		System.out.println("access_token==========="+access_token);
		HttpSession sen = getSession();
		sen.setAttribute("access_token", access_token);
		
		boolean is_right = userService.query(name,password);
		
		setAttr("is_right",is_right);
		renderJson();
	}
	
	/**
	 * 判断名字是否存在
	 */
	public void signin(){
		
		String name = getPara("name");
		//调用service
		boolean is_sign = userService.query(name);
		
		setAttr("is_sign",is_sign);
		renderJson();
	}
	/**
	 * 
	 */
	public void sign()
	{
		String name = getPara("name");
		String password = getPara("password");
		String phone = getPara("phone");
		String sex = getPara("sex");
	    String school_id = getPara("school_id");
	    
	    String academy_id = getPara("academy_id");
	    String major_id = getPara("major_id");
	  //  int area_id = getParaToInt("area_id");
	    String dorm_id = getPara("dorm_id");	
	    
	    int number = userService.sign(name, password, sex, phone, Integer.parseInt(school_id), Integer.parseInt(academy_id), Integer.parseInt(major_id), Integer.parseInt(dorm_id));
	    boolean is_success;
	    if(number ==0)
	    {
	    	is_success =false;
	    }else
	    {
	    	this.getSession().setAttribute(Constants.User_SESSION,number);
	    	is_success = true;
	    }
	    setAttr("is_success",is_success);
		renderJson();	    
	}
	
	public void search_academy()
	{
		 int school_id = getParaToInt("school_id"); 
		 List<Academy> list = userService.Search_Academy(school_id);
		 setAttr("list",list);
	     renderJson();	
	}
	
	public void search_major()
	{
		 int school_id = getParaToInt("school_id"); 
		 List<Major> list = userService.Search_Major(school_id);
		 setAttr("list",list);
	     renderJson();	
	}
	
	public void search_area()
	{
		 int school_id = getParaToInt("school_id"); 
		 List<Area> list = userService.Search_Area(school_id);
		 setAttr("list",list);
	     renderJson();	
	}
	
	public void search_dorm()
	{
		 int school_id = getParaToInt("school_id"); 
		 int area_id = getParaToInt("area_id"); 
		 List<Dorm> list = userService.Search_Dorm(school_id,area_id);
		 setAttr("list",list);
	     renderJson();	
	}
	
	public void get_ma() throws InterruptedException, ClientException
	{
		//System.out.println("1231213123123");
		String number = getPara("number");//电话号码
		String yanzhengma = MessageUtils.getCode(number); 
		
		setAttr("yanzhengma",yanzhengma);
		renderJson();
	}
	public void renderLogin(){
		render("login.html");
	}
}
